// pages/user/user.js
var ports = require("../../utils/ports.js")
var utils = require("../../utils/util.js");
// 获取应用实例
const app = getApp()

Component({
  /**
   * 页面的初始数据
   */
  data: {
    botH: '0px',
    collectNums: 0,//收藏数
    goodsShopBrowseNum: 0,//足迹
    pointTotal: 0,//积分
    cashBalance: 0,//余额
    monthBalance:0,//月结额
    memberBonusNum: 0,//优惠券
    cardBonusNum: 0,//卡券
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  }, 
  attached: function () {
    this.setData({
      botH: app.globalData.bottomHeight + 'px'
    })
    //初始化用户信息
    this.init();
  },
  methods: {
    enterAni() {
      var that = this
      //进入动画
      var animation = wx.createAnimation({
        duration: 1000,
        timingFunction: 'ease',
        delay: 0
      });
      animation.opacity(1).translateY(0).step()
      that.setData({
        ani: animation.export()
      })
    },
    outAni() {
      var that = this
      //清除动画
      var animation = wx.createAnimation({
        duration: 10,
        timingFunction: 'ease',
        delay: 0
      });
      animation.opacity(0.2).translateY(30).step()
      that.setData({
        ani: animation.export()
      })
    },
    //用户充值
    torecharge(){
      wx.navigateTo({
        url: "/pages/recharge/recharge",
      })
    },
    tobalance(){
      wx.navigateTo({
        url: '/pages/balance/balance',
      })
    },
    topoint(){
      wx.navigateTo({
        url: '/pages/pointList/pointList',
      })
    },
    toorder: function (e) {
      wx.navigateTo({
        url: '/pages/order/order?status=' + e.currentTarget.dataset.status,
      })
    },
    toaddress: function () {
      wx.navigateTo({
        url: '/pages/address/address',
      })
    },
    init() {
      var that = this
      var sessionID = wx.getStorageSync('USER_SESSIONID')
      var member = wx.getStorageSync('USER_MEMBER')
      if (sessionID && !member) {
        that.getOneMemberDetail()
      } else if (member) {
        that.setData({
          member: member
        })
      }
      if (sessionID) {
        that.getMemberStatisticsInfo()
      }
    },
    getOneMemberDetail() {
      let that = this
      ports.ModuleMember.getOneMemberDetail({}, that, res => {
        that.setData({
          member: res.data.body.data
        })
        wx.setStorageSync("USER_MEMBER", res.data.body.data)
      })
    },
    toQrcode(e){
      wx.navigateTo({
        url: '/pages/qrCode/qrCode',
      })
    },
    getUserProfile(e) {
      let that = this
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: (res) => {
          if (res.userInfo != null) {
            wx.setStorageSync("USER_DEFAULT", {
              avatarUrl: res.userInfo.avatarUrl,
              nickName: res.userInfo.nickName
            })
            utils.doLoginOther((data) => {
              that.setData({
                member: data
              })
              that.init()
            })
          }
        }
      })
    },
    getMemberStatisticsInfo() {
      let that = this
      ports.ModuleMember.getMemberStatisticsInfo({
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            collectNums: res.data.body.data.goodsShopCollectNum,
            goodsShopBrowseNum: res.data.body.data.goodsShopBrowseNum,
            pointTotal: res.data.body.data.point,//积分
            cashBalance: res.data.body.data.cashBalance.toFixed(2),//余额
            cashTotalRecharge: res.data.body.data.cashTotalRecharge.toFixed(2),//累计充值
            monthBalance: res.data.body.data.monthBalance.toFixed(2),//月结额
            memberBonusNum: res.data.body.data.memberBonusNum,//优惠券
            cardBonusNum: res.data.body.data.memberCardNum,//卡券
            rankName: res.data.body.data.rankName//会员等级
          })
        } else {
          wx.showToast({
            title: res.data.header.msg,
            icon: 'none'
          })
        }
      })
    },
    toColllectsList() {
      wx.navigateTo({
        url: '/pages/collectList/collectList',
      })
    },
    togoodsShopBrowse() {
      wx.navigateTo({
        url: '/pages/goodsShopBrowse/goodsShopBrowse',
      })
    },
    //我的优惠券
    tomycoupon() {
      wx.navigateTo({
        url: '/pages/mycoupon/mycoupon',
      })
    },
    //清除缓存
    clearStorage() {
      wx.clearStorage({
        success: (res) => {
          wx.showToast({
            title: '清除成功',
            icon: 'none'
          })
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/user/user',
            })
          }, 500);
        },
        fail: () => {
          wx.showToast({
            title: '清除失败，请重试！',
          })
        }
      })
    },
    //购买卡券
    toCardList() {
      wx.navigateTo({
        url: '/pages/cardList/cardList',
      })
    },
    //我的卡券
    toMyCardList() {
      wx.navigateTo({
        url: '/pages/myCardList/myCardList',
      })
    },
    onPullDownRefresh() {
      var that = this;
      ports.ModuleAll.PullDownRefresh(that, res => {
        that.init()
      })
    }
  },
  pageLifetimes: {
    show() {
      //tabbar定位
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 3
        })
      }
      //初始化用户信息
      this.init();
      this.enterAni()
    },
    hide() {
      this.outAni()
    }
  }
})